package com.sundog.sundroid;

import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.sundog.sundroid.google.GoogleService;

/**
 * Class for displaying the card selection screen to the user
 * 
 * @author Matt
 *
 */
public class UserScreen extends Activity implements OnClickListener {
	
	/** Spreadsheet ID */
	public static String id = "";
	/** Array of story names */
	public static String [] storyNames;
	/** Array of story details */
	public static String [] detailed;
	/** Array of story ID's */
	public static String [] storyID;
	/** Array of user names */
	public static String [] userNames;
	/** Map of users */
	public static Map<String, String> users;
	/** Array of users' online status */
	public static String [] userOnlineStatus;
	/** Map of storys */
	public static Map<String, String> storys;
	/** Current story ID */
	public static String storyId;
	/** Current story ID */
	public static int stid;
	/** Map of story details */
	public static Map<String, String> details;
	/** Array of stories */
	public static String [][] story1 = new String [3][200];
	/** Variable representing this instance of the UserScreen */
	UserScreen screen = this;
	
	/*
	 * Creates an Array 3 deep and 200 in each 
	 * 0 = short description "what the moderator chooses to vote on
	 * 1 = detailed description of the story
	 * 2 = what the group voted on for that story
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card);
		
		// initialize variables
		id = this.getIntent().getStringExtra("SID");
		storys = GoogleService.returnStorys(Login.SpreadId);
		details = GoogleService.returnDetails(Login.SpreadId);
		storyNames = storys.keySet().toArray(new String[storys.size()]);
		storyID = storys.values().toArray(new String[storys.size()]);
    	detailed = details.keySet().toArray(new String[details.size()]);
		
		
		TextView details = (TextView) this.findViewById(R.id.story_info);
		String storyID = this.getIntent().getStringExtra("SID");
		Log.d("STORY", storyID);
		details.setText("\n Story =  " + detailed[Wait.storyID]);
			
			// Set up click listeners for buttons
			View zeroButton = this.findViewById(R.id.zero_button);
	        zeroButton.setOnClickListener(this);
	        View oneButton = this.findViewById(R.id.one_button);
	        oneButton.setOnClickListener(this);
	        View twoButton = this.findViewById(R.id.two_button);
	        twoButton.setOnClickListener(this);
	        View threeButton = this.findViewById(R.id.three_button);
	        threeButton.setOnClickListener(this);
	        View fiveButton = this.findViewById(R.id.five_button);
	        fiveButton.setOnClickListener(this);
	        View eightButton = this.findViewById(R.id.eight_button);
	        eightButton.setOnClickListener(this);
	        View thirteenButton = this.findViewById(R.id.thirteen_button);
	        thirteenButton.setOnClickListener(this);
	        View twentyButton = this.findViewById(R.id.twenty_button);
	        twentyButton.setOnClickListener(this);
	        View fortyButton = this.findViewById(R.id.forty_button);
	        fortyButton.setOnClickListener(this);
	        View hundredButton = this.findViewById(R.id.hundred_button);
	        hundredButton.setOnClickListener(this);
	        View questionButton = this.findViewById(R.id.question_button);
	        questionButton.setOnClickListener(this);
	        View infintyButton = this.findViewById(R.id.infinty_button);
	        infintyButton.setOnClickListener(this);	    
	}
	
	/**
	 * Process button clicks
	 */
	public void onClick(View v) {
    	switch (v.getId()) {
    	case R.id.zero_button:
    		confirmationAlert("0");
    		break;
    	case R.id.one_button:
    		confirmationAlert("1");
    		break;
    	case R.id.two_button:
    		confirmationAlert("2");
    		break;
    	case R.id.three_button:
    		confirmationAlert("3");
    		break;
    	case R.id.five_button:
    		confirmationAlert("5");
    		break;
    	case R.id.eight_button:
    		confirmationAlert("8");
    		break;
    	case R.id.thirteen_button:
    		confirmationAlert("13");
    		break;
    	case R.id.twenty_button:
    		confirmationAlert("20");
    		break;
    	case R.id.forty_button:
    		confirmationAlert("40");
    		break;
    	case R.id.hundred_button:
    		confirmationAlert("100");
    		break;
    	case R.id.question_button:
    		confirmationAlert("?");
    		break;
    	case R.id.infinty_button:
    		confirmationAlert("∞");
    		break;
    	}
    }

	/**
	 * Confirm user vote
	 * 
	 * @param vote
	 * 					the users vote
	 */
	private void confirmationAlert(final String vote) {
		new AlertDialog.Builder(this)
        .setTitle("Is this your vote?")
        .setMessage(vote)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {	                	
                	Intent l = new Intent(screen, ResultsWaitingScreen.class);
            		Log.d("SID", id);
            		l.putExtra("userVote", vote);
            		l.putExtra("SID", id);
            		l.putExtra("username", screen.getIntent().getStringExtra("username"));
                	startActivity(l);
                }
        }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        }).show();
	}  
}
